package com.atguigu.gulimall.order.lisener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

@RabbitListener( queues = "order.release.order.queue")
public class OrderListener {

    @Autowired
    OrderService orderService;

    /**
     *   延迟队列监听到过期的 消息
     * @param orderEntity
     * @param message
     * @param channel
     */
    @RabbitHandler
    public void handleReleaseOrder(OrderEntity orderEntity, Message message, Channel channel) throws IOException {
          try {
              orderService.handleReleaseOrder(orderEntity);
              channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
          }catch (Exception e){
              // 发生异常 拒绝处理消息  让消息重新 回队
              channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
          }
    }









}
